• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help to fix the Audio Music Player Errors!!

Hello good morning for everyone I´m a beginner on the Android Studio, and i need a help for you to place well the app, that means working perfectly. It´s a good app, and it could serve me as a example to understand its funcionality.

I´m building an audio music player app., but on my MainActivity.java file ,


In the code i faced with some mistakes, see below the images that i sent you. How could you fix them?


MainActivity.java


Java:
package com.example.musicaudioplayer;


import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentPagerAdapter;

import androidx.viewpager.widget.ViewPager;


import android.content.Context;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.widget.Toast;


import com.google.android.material.tabs.TabLayout;


import java.util.ArrayList;

import java.util.jar.Manifest;


public class MainActivity extends AppCompatActivity {

    public static final int REQUEST_CODE=1;

    ArrayList<MusicFiles>musicFiles;

    [USER=1021285]@override[/USER]

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        permission();

        initViewpager();

    }


    private void permission() {

        if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)

        != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(MainActivity.this, new String[]{

                            Manifest.permission.WRITE_EXTERNAL_STORAGE}

                    , REQUEST_CODE);

        }

                else

        {

            Toast.makeText(this, "Permission Granted!", Toast.LENGTH_SHORT).show();

            musicFiles = getAllAudio(this);

        }

    }

    [USER=1021285]@override[/USER]

        public void onRequestPermissionsResult (int requestCode,@NonNull String[]permissions,

                                                @NonNull int[] grantResults){

        super.onRequestPermissionsResult(requestCode,permissions,grantResults);

        if (requestCode==REQUEST_CODE)

        {

            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){

                {

                  //Do whatever you want permission related

                    Toast.makeText(this, "Permission Granted !", Toast.LENGTH_SHORT).show();

                    musicFiles =getAllAudio(this);


                }

                else

                {

                    ActivityCompat.requestPermissions(MainActivity.this, new String []{Manifest.permission.WRITE_EXTERNAL_STORAGE,

                            REQUEST_CODE);

                    }

                    }

            }


    private void initViewpager(){

        ViewPager viewPager =findViewById(R.id.viewpager);

        TabLayout tabLayout = findViewById(R.id.tab_layout);

        ViewpagerAdapter viewPagerAdapter = new ViewpagerAdapter(getSupportFragmentManager());

        viewPagerAdapter.addFragments(new SongsFragment(), "Songs");

        viewPagerAdapter.addFragments(new AlbumFragment(),"Album");

        viewPager.setAdapter(viewPagerAdapter);

        tabLayout.setupWithViewPager(viewPager);

    }

    public static class ViewpagerAdapter extends FragmentPagerAdapter{


        private ArrayList<Fragment> fragments;

        private ArrayList<String> titles;

        public ViewpagerAdapter (@NonNull FragmentManager fm){

            super(fm);

            this.fragments = new ArrayList<>();

            this.titles = new ArrayList<>();

        }

        void addFragments (Fragment fragment, String title){

            fragments.add(fragment);

            titles.add(title);


        }


        @NonNull

        [USER=1021285]@override[/USER]

        public Fragment getItem (int position){

            return fragments.get(position);

        }

        [USER=1021285]@override[/USER]

        public int getCount(){

            return fragments.size();

        }


        [USER=1996173]@nullable[/USER]

        [USER=1021285]@override[/USER]

        public CharSequence getPageTitle(int position){

            return titles.get(position);

        }

    }

}

    }

}

public static ArrayList <cMusicFiles> getAllAudio(Context context){

  ArrayList<MusicFiles> tempAudioList = new ArrayList<>();

    Uri uri= MediaStore.Audio.EXTERNAL_CONTENT_URI;

    String[] projection = (

            MediaStore.Audio.Media.ALBUM,

            MediaStore.Audio.Media.TITLE,

            MediaStore.Audio.Media.DURATION,

            MediaStore.Audio.Media.DATA, // FOR PATH

            MediaStore.Audio.Media.ARTIST,

};

        Cursor cursor = context.getContentResolver().query(uri, projection, null,null,null);

        if (cursor!=null)

                {

                while(cursor.moveToNext())

                {

                String album = cursor.getString(0);

                String title = cursor.getString(1);

                String duration = cursor.getString(2);

                String path = cursor.getString(3);

                String artist = cursor.getString(4);


                MusicFiles musicFiles = new MusicFiles(path, title,artist,album,duration);

                //take log.e for check

                Log.e("Path: " + path, "Album : " + album);

                tempAudioList.add(musicFiles)

                    }

                cursor.close();

                 }

        return tempAudioList;

                }

                }


unknown1.png


unknown2.png


AndroidManifest.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.musicaudioplayer">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="[USER=22138]@Mipmap[/USER]/ic_launcher"
        android:label="[USER=696546]@String[/USER]/app_name"
        android:roundIcon="[USER=22138]@Mipmap[/USER]/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="[USER=19691]@Style[/USER]/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>


AlbumFragment.java

Java:
package com.example.musicaudioplayer;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
* A simple {@link Fragment} subclass.
*/
public class AlbumFragment extends Fragment {

    public AlbumFragment() {
        // Required empty public constructor
    }


    [USER=1021285]@override[/USER]
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_album, container, false);
        return view;
    }
}

MusicFiles.java

Java:
package com.example.musicaudioplayer;

public class MusicFiles {
    private String path;
    private String title;
    private String artist;
    private String album;
    private String duration;

    public MusicFiles(String path, String title, String artist, String album, String duration) {
        this.path = path;
        this.title = title;
        this.artist = artist;
        this.album = album;
        this.duration = duration;
    }

    public MusicFiles() {
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }
}


fragment_album.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".AlbumFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="[USER=696546]@String[/USER]/hello_blank_fragment" />

</FrameLayout>

SongsFragment.java

Java:
package com.example.musicaudioplayer;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
* A simple {@link Fragment} subclass.
*/
public class SongsFragment extends Fragment {

    public SongsFragment() {
        // Required empty public constructor
    }


    [USER=1021285]@override[/USER]
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_songs, container, false);
        return view;
    }
}

fragment_songs.xml



Code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".SongsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="[USER=696546]@String[/USER]/hello_blank_fragment" />

</FrameLayout>


Ps.: I send also the ZIP file about the audio music player app, My Android version is below:

android_studio_version.png




Another trouble is the emulator that in my pc doesn´t work, i think it´s because my Pc doesn´t have the Intel processor but the AMD Processor. Could you help to fix this problem too? see the images

unknown3.png



unknown4.png



Could anyone, help me, please to fix this problems?

I expect your answer as soon as possible.

Best regards

José Moreira
 

Attachments

Last edited by a moderator:
I moved your thread to the development area for better views. :) I also added code tags for readability. Good luck!! :)

Code Tag Usage:
[code]Line 1
Line 2
Line 3
Maintains white space
Doesn't turn :) into emoji
etc...
[/code]
Code:
Line 1
Line 2
Line 3
Maintains white space
Doesn't turn :) into emoji
etc...
 
Maybe you can try another idea and use a third-party tool. In order to solve the incompatibility problem, Tunelf Apple Music Converter was developed. It can convert various audio files from one format to another in one click. Therefore, you can play the audio on any audio music player.
 
Back
Top Bottom