在资源文件夹中播放mp3文件无效

时间:2018-01-29 06:48:38

标签: android android-mediaplayer

我想在res / raw文件夹中播放一个mp3文件 但我得到错误"错误(1,-2147483648)"和mp.prepare()上的IOException

我的代码

matplotlib

我也试过

import matplotlib.pyplot as plt
from numpy import arange, meshgrid

delta = 0.025
x_range = arange(-20.0, 20.0, delta)
y_range = arange(-20.0, 20.0, delta)
X, Y = meshgrid(x_range,y_range)

fig=plt.figure()
ax=fig.add_subplot(111)
ax.set_xlim(xmin=-6, xmax=6)
ax.set_ylim(ymin=-4, ymax=4)
ax.axhline(y=0, color='k',linewidth=1)
ax.axvline(x=0, color='k',linewidth=1)
# F is one side of the equation, G is the other
F = (X**2)/2.0+(Y**2)
colrs_tuple=('k','c','c','k')
#C = range(6,14,3)
C = (2,6,9,12)
#print(type(C))
CS = plt.contour(X, Y, F, C, colors=colrs_tuple)
labels = plt.clabel(CS)
xy = [t.get_position() for t in labels]
print(xy)

F=Y
G=X**2
C = (-5,-3,-1,1,3,5)
##for c1 in C:
##    CS=plt.contour(X, Y, F/G- c1,[0], colors='c',linestyles='dashed')
##    labels = plt.clabel(CS)
##    xy = [t.get_position() for t in labels]
##    print(xy)
##plt.show()
CS1=plt.contour(X, Y, F/G,levels=(-15,-3,-1,-0.5,0.5,1,3,15), colors='c',linestyles='dashed')
labels1 = plt.clabel(CS1)
plt.show()

我试过的另一个解决方案

try {
        MediaPlayer mPlayer = MediaPlayer.create(NavigationHome.this, R.raw.notfy);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

这些对我来说也没有用。

3 个答案:

答案 0 :(得分:3)

如果你可以在你的问题中发布StackTrace,那将会有更多帮助。

但是,根据您问题中的信息,以下代码可用于播放原始资源文件夹中的媒体文件。

如果您使用 create()方法,则会在内部调用 prepare(),您无需显式调用它。

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.notify);
mediaPlayer.start();

但是,需要考虑的是,prepare()通常会抛出 IllegalStateException ,在您的情况下,您将获得 IOException 。因此,值得检查文件是否实际存在于原始文件夹中和/或文件是否已损坏。

答案 1 :(得分:1)

尝试在准备媒体播放器或设置数据源之前初始化媒体播放器

从外部目录播放

String filePath = Environment.getExternalStorageDirectory()+"/folderName/yourfile.mp3";
mediaPlayer = new  MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();   
mediaPlayer.start()

从原始文件夹

 MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.song);
 mediaPlayer.start();

答案 2 :(得分:0)

试试这个

String fname="your_filename";
int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.start();
相关问题