我试图创建一个小型媒体播放器。因此我写了这段代码来播放位于assets文件夹中的.aac文件:
public class FragmentDominik extends Fragment {
Button button;
View view;
//MainActivity mA = new MainActivity();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dominik,container, false);
button = (Button) view.findViewById(R.id.dominikButton1);
Log.v("Syx","In Activity");
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.v("Syx","Clicked");
try {
MediaPlayer player = new MediaPlayer();
AssetFileDescriptor descriptor = view.getContext().getAssets().openFd("dome__heeey.aac");
/* if (mA.mP.isPlaying()) {
mA.mP.reset();
}*/
Log.v("Syx","Playing");
player.setDataSource(descriptor.getFileDescriptor());
Log.v("Syx", "Playing 2");
player.prepare();
Log.v("Syx", "Playing 3");
player.start();
Log.v("Syx", "Playing 4");
} catch (IOException e) {
}
}
});
return view;
}
}
但由于某些原因,当我尝试播放音乐时,它会给我一个错误,所以在Log"播放2"之后出现错误,所以错误与player.prepare有关();.我不知道是什么导致了这个错误,所以任何帮助表示赞赏:) 编辑:Stacktrace
01-24 16:21:18.044 730-730/com.example.admin.soundboard V/Syx: Clicked
01-24 16:21:18.045 730-730/com.example.admin.soundboard V/Syx: Playing
01-24 16:21:18.052 730-730/com.example.admin.soundboard V/Syx: Playing 2
01-24 16:21:18.060 730-741/com.example.admin.soundboard E/MediaPlayer: error (1, -2147483648)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: java.io.IOException: Prepare failed.: status=0x1
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.media.MediaPlayer._prepare(Native Method)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.media.MediaPlayer.prepare(MediaPlayer.java:1163)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at com.example.admin.soundboard.FragmentDominik$1.onClick(FragmentDominik.java:44)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.view.View.performClick(View.java:5198)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.view.View$PerformClick.run(View.java:21147)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.os.Looper.loop(Looper.java:148)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-24 16:21:18.061 730-730/com.example.admin.soundboard W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)