简体   繁体   English

触摸按钮时如何使用MediaPlayer播放Mp3文件

[英]How to play a Mp3 file using MediaPlayer when touch a Button

I get the error below every time I try to play the song, I successfully play the song when add the code in the main activity although when try playing in other of the activity the app crash and I just want to know why this happen and how I could solve the issue. 每次尝试播放歌曲时,都会收到以下错误消息;在主活动中添加代码时,我成功播放了歌曲;尽管在其他活动中尝试播放时,应用崩溃了,我只想知道为什么会这样,以及如何发生我可以解决这个问题。

Error: 错误:

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: 代码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: algenisromero@gmail.com"));
                sendFeedbackIntent.putExtra(Intent.EXTRA_SUBJECT, "Phase UP Support.");
                if (sendFeedbackIntent.resolveActivity(getPackageManager()) != null) {
                    startActivity(sendFeedbackIntent);
                }
            }
        });

    }
}

Code AviationSongActivity.java: 代码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 代码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>

You can find the full code of the app in my github account PhaseUp App 您可以在我的github帐户PhaseUp App中找到该应用程序的完整代码

Thanks for your help! 谢谢你的帮助!

Update your MP3 file so it is not empty or corrupt. 更新您的MP3文件 ,使其不为空或损坏。

The code looks okay, otherwise. 该代码看起来还可以,否则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM