Support to fix/solve the errors of MainActivity of Audio Player Music App!

Good day, for everyone.

I am very new at coding and is studying online. I’m busy with an audio music palyer app…but have run into some problems, 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 that i send you below i faced with some mistakes.

How could I fix them?

MainActivity.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;

    @Override

    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);

        }

    }

    @Override

        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

        @Override

        public Fragment getItem (int position){

            return fragments.get(position);

        }

        @Override

        public int getCount(){

            return fragments.size();

        }


        @Nullable

        @Override

        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;

                }

                }

AndroidManifest.xml

<?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="@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>

AlbumFragment.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
    }


    @Override
    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

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

<?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="@string/hello_blank_fragment" />
    </FrameLayout>

SongsFragment.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
    }


    @Override
    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

<?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="@string/hello_blank_fragment" />

</FrameLayout>


tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:centerColor="@color/colorPrimaryDark" android:angle="0"/>

</shape><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:centerColor="@color/colorPrimaryDark" android:angle="0"/>

</shape>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1B1A1C</color>
    <color name="colorPrimaryDark">#151515</color>
    <color name="colorAccent">#FFFFFF</color>
</resources>

strings.xml

<resources>
    <string name="app_name">Musicaudioplayer</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

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?

The error is “Intel HAXM is required to run this AVD. Install…” folllow by the error “Failed to install HAXM …”

My Android Studio version is 3.6.3.

I need your support, please to understand so in-depth this software and the way of the funcionality of this application.

Have a nice weekend.

Best regards
José Moreira

Hello there,

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).