Ich bekomme den Fehler unten jedes Mal, wenn ich versuche, den Song zu spielen, ich den Song erfolgreich spielen, wenn Sie den Code in der Hauptaktivität hinzufügen, obwohl versuchen, in anderen der zu spielen Die App stürzt ab und ich möchte nur wissen, warum das passiert und wie ich das Problem lösen könnte.So spielen Sie eine MP3-Datei mit MediaPlayer, wenn Sie einen Button berühren
Fehler:
E/MediaPlayer: error (1, -2147483648)
D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1158)
at android.media.MediaPlayer.create(MediaPlayer.java:944)
at android.media.MediaPlayer.create(MediaPlayer.java:915)
at com.example.android.phaseup.AviationSongActivity.onCreate(AviationSongActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.phaseup, PID: 1644
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.example.android.phaseup.AviationSongActivity$1.onClick(AviationSongActivity.java:36)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.
-Code MainActivity.java:
package com.example.android.phaseup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Call BlueBook Activity
TextView blueBook = (TextView) findViewById(R.id.blue_book);
//Set a click listener in that View
blueBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent blueBookIntent = new Intent(MainActivity.this, BlueBookActivity.class);
startActivity(blueBookIntent);
}
});
//Call Aviation Song Activity
TextView aviationSong = (TextView) findViewById(R.id.aviation_songs);
//Set a click listener in that View
aviationSong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent aviationIntent = new Intent(MainActivity.this, AviationSongActivity.class);
startActivity(aviationIntent);
}
});
//Call Soldier's Creed Activity
TextView soldierCreed = (TextView) findViewById(R.id.soldier_creed);
//Set a click listener in that View
soldierCreed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent soldierCreedIntent = new Intent(MainActivity.this, SoldierCreedActivity.class);
startActivity(soldierCreedIntent);
}
});
//Call Room Inspection Activity
TextView roomInspection = (TextView) findViewById(R.id.room_inspection);
//Set a click listener in that View
roomInspection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent roomInspectionIntent = new Intent(MainActivity.this, RoomInspectionActivity.class);
startActivity(roomInspectionIntent);
}
});
//Call Wall Locker Inspection Activity
TextView wallLocker = (TextView) findViewById(R.id.wall_Locker_inspection);
//Set a click listener in that View
wallLocker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent wallLockerIntent = new Intent(MainActivity.this, WallLockerActivity.class);
startActivity(wallLockerIntent);
}
});
//Call ASU Inspection Activity
TextView asuInspection = (TextView) findViewById(R.id.asu_inspection);
//Set a click listener in that View
asuInspection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent asuIntent = new Intent(MainActivity.this, ASUActivity.class);
startActivity(asuIntent);
}
});
//Feedback button.
TextView sendFeedBack = (TextView) findViewById(R.id.send_feedback);
sendFeedBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendFeedbackIntent = new Intent(Intent.ACTION_SENDTO);
sendFeedbackIntent.setData(Uri.parse("mailto: [email protected]"));
sendFeedbackIntent.putExtra(Intent.EXTRA_SUBJECT, "Phase UP Support.");
if (sendFeedbackIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendFeedbackIntent);
}
}
});
}
}
-Code AviationSongActivity.java:
package com.example.android.phaseup;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AviationSongActivity extends AppCompatActivity {
//Media Player variable.
MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aviation_song);
//Create and Upload the MP3 file.
mMediaPlayer = MediaPlayer.create(this, R.raw.aviationsong);
//The aviation song text show in the screen.
TextView textViewAviation = (TextView) findViewById(R.id.textview_aviation);
textViewAviation.setText("High above the best, high above the best \n\n" +
"We are Army Aviation USA, \n\n" +
"proud and strong We meet the test \n\n" +
"Skies filled with thunder \n\n" +
"Wearing silver wings upon our chest \n\n" +
"We meet the needs of Ground Command \n\n" +
"As we aid the Nation's quest \n\n" +
"Army Aviation, flying high above the best!");
//Listener to play sound when user touched.
Button playButton = (Button) findViewById(R.id.play_sound);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.start();
}
});
}
}
-Code activity_aviation_song.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/activity_aviation_song"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.phaseup.AviationSongActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:background="@color/colorPrimaryDark">
<Button
android:id="@+id/play_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="PLAY"
android:textSize="56sp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:background="@null"/>
</RelativeLayout>
<TextView
android:id="@+id/textview_aviation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:gravity="center"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
Sie können den vollständigen Code der App in meinem GitHub Konto PhaseUp App
Danke für Ihre Hilfe finden!
Sie sollten Ihren MediaPlayer auch mit folgendem Code vorbereiten: mMediaPlayer.prepare() und danach können Sie den Benutzer darauf klicken und abspielen. – Amir
@Amir Warum? [Die Dokumentation] (https://developer.android.com/guide/topics/media/mediaplayer.html#mediaplayer) sagt, dass Sie anfangen können, Medien nur mit Start –
zu spielen @ cricket_007 Sie haben Recht. Ich weiß nicht, warum dieser Kommentar in der Dokumentation erwähnt, aber in meinem Fall, wenn ich prepareAsync Problem gelöst. – Amir