在Android Studio中基本音乐播放器的音频问题。

huangapple 未分类评论63阅读模式
英文:

Problem with Audio in a basic music player in Android Studio

问题

the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.basicmusicplayer">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

the xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/greetings"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="221dp"
        android:layout_height="298dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="104dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:srcCompat="@android:drawable/ic_media_play" />

    <Button
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/pause"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/stop"
        app:layout_constraintHorizontal_bias="0.565"
        app:layout_constraintStart_toEndOf="@+id/play"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintVertical_bias="0.205" />

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:text="@string/play"
        app:layout_constraintBaseline_toBaselineOf="@+id/pause"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/stop"
        app:layout_constraintBaseline_toBaselineOf="@+id/pause"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

the java code

package com.example.basicmusicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        player = MediaPlayer.create(this, R.raw.adele);

        Button play_music = findViewById(R.id.play);
        play_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Playing sound", Toast.LENGTH_SHORT).show();
                player.start();
            }
        });

        Button pause_music = findViewById(R.id.pause);
        pause_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Paused sound", Toast.LENGTH_SHORT).show();
                player.pause();
            }
        });

        Button stop_music = findViewById(R.id.stop);
        stop_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Stopped sound", Toast.LENGTH_SHORT).show();
                player.stop();
            }
        });
    }
}
英文:

I created a basic music player app in Android Studio with the music file stored in the raw directory under resources and Mp3 format.
the issue is that though the toast inside the event listener gets displayed , the audio is not playing except once(i really do not know how).
the code is as below.

the manifest file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.basicmusicplayer&quot;&gt;

    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;

        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

the xml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:layout_gravity=&quot;center&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/textView&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;@string/greetings&quot;
        android:textSize=&quot;30sp&quot;
        android:textStyle=&quot;bold&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;ImageView
        android:id=&quot;@+id/imageView&quot;
        android:layout_width=&quot;221dp&quot;
        android:layout_height=&quot;298dp&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        android:layout_marginTop=&quot;104dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:layout_marginRight=&quot;8dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.505&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot;
        app:srcCompat=&quot;@android:drawable/ic_media_play&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/pause&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        android:layout_marginTop=&quot;8dp&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:layout_marginRight=&quot;8dp&quot;
        android:layout_marginBottom=&quot;8dp&quot;
        android:text=&quot;@string/pause&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toStartOf=&quot;@+id/stop&quot;
        app:layout_constraintHorizontal_bias=&quot;0.565&quot;
        app:layout_constraintStart_toEndOf=&quot;@+id/play&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/imageView&quot;
        app:layout_constraintVertical_bias=&quot;0.205&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/play&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;8dp&quot;
        android:layout_marginLeft=&quot;8dp&quot;
        android:text=&quot;@string/play&quot;
        app:layout_constraintBaseline_toBaselineOf=&quot;@+id/pause&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/stop&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;8dp&quot;
        android:layout_marginRight=&quot;8dp&quot;
        android:text=&quot;@string/stop&quot;
        app:layout_constraintBaseline_toBaselineOf=&quot;@+id/pause&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

the java code

package com.example.basicmusicplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
         an object of class MediaPlayer created named player
         a mp3 file is added to the object using create method*/
        player = MediaPlayer.create(this, R.raw.adele);

//        setting up  inline event listeners to the button
//        first comes the play button
        Button play_music = findViewById(R.id.play);
        play_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), &quot;Playing sound&quot;,Toast.LENGTH_SHORT).show();
                player.start();

            }
        });

//        adding event listener on pause button
        Button pause_music = findViewById(R.id.pause);
        pause_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), &quot;paused sound&quot;,Toast.LENGTH_SHORT).show();
                player.pause();

            }
        });

//        adding event listener for stop button
        Button stop_music = findViewById(R.id.stop);
        stop_music.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), &quot;stopped sound&quot;,Toast.LENGTH_SHORT).show();
                player.stop();

            }
        });



    }
}

答案1

得分: 0

private MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button b = (Button) findViewById(R.id.button1);

    final TextView t = (TextView) findViewById(R.id.textView1);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopPlaying();
            mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
            mp.start();
        }

    });
}

private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
   }
}
英文:
 private MediaPlayer mp;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button) findViewById(R.id.button1);
       
        final TextView t = (TextView) findViewById(R.id.textView1);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                stopPlaying();
                mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.far);
                mp.start();
            }

        });
    }

    private void stopPlaying() {
        if (mp != null) {
            mp.stop();
            mp.release();
            mp = null;
       }
    }

huangapple
  • 本文由 发表于 2020年5月5日 13:55:46
  • 转载请务必保留本文链接:https://java.coder-hub.com/61606642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定